/*********************************************************************************
 *      Copyright:  (C) 2021 litupei<lismilepei@163.com>
 *                  All rights reserved.
 *
 *       Filename:  main.h
 *    Description:  This file is main.h
 *                 
 *        Version:  1.0.0(2021年08月10日)
 *         Author:  litupei <lismilepei@163.com>
 *      ChangeLog:  1, Release initial version on "2021年08月10日 17时23分47秒"
 *                 
 ********************************************************************************/

#include "main.h"

#define PATH_ini "../etc/mqtt_config.ini"

int	g_run = 0;
void sig_handler(int signum)
{
	if (signum == SIGINT)
	{
		g_run = 1;
	}
}

int main(int argc, char **argv)
{
	int		rv = 0;
	mqtt_cxt_t	mqtt;
	pthread_attr_t 	thread_attr;
	pthread_t            	sub_tid;
	pthread_t            	pub_tid;

	memset(&mqtt, 0, sizeof(mqtt));
	rv = ini_mqttmsg(PATH_ini,&mqtt);
	if( ini_mqttmsg(PATH_ini,&mqtt) < 0 )
	{
		printf("ini setting failture : %s! \n", strerror(errno));
		return -1;
	}
    	printf("ini setting successfully! \n");

    	rv = zlog_set(mqtt.ZLOG_CONF_PATH, &mqtt.zlog);
   	 if(rv < 0)
    	{
        	printf("zlog init failure! \n");
        	return -1;
    	}
    	printf("zlog init successfully! \n");

	signal(SIGINT, sig_handler);

	if( mosquitto_lib_init() != MOSQ_ERR_SUCCESS )
	{
		zlog_error(mqtt.zlog, "the mosquitto init failture! :%s", strerror(errno));
		return -1;
	}
    	zlog_info(mqtt.zlog, "the mosquitto init successfully!");

#if 1
    	/*setting publish_thread*/
    	rv = thread_start(&pub_tid, pub_connect, (void *)&mqtt);
    	if ( rv < 0 )
    	{
        	zlog_error(mqtt.zlog,"creatting pub_Thread failure : %s\n", strerror(errno));
        	return rv;
    	}
    	zlog_info(mqtt.zlog, "creatting sub_Thread success\n");
#endif

#if 1
    	/*setting subscript_thread*/
    	rv = thread_start(&sub_tid, sub_connect, (void *)&mqtt);
    	if ( rv < 0 )
    	{
        	zlog_error(mqtt.zlog, "creatting sub_Thread failure\n");
        	return rv;
    	}
    	zlog_info(mqtt.zlog, "creatting sub_Thread success\n");

#endif

	while ( !g_run )
	{
		;
	}

    	zlog_fini();
	return 0;
}


